package com.lablzh.gdxy.service.impl;

import com.lablzh.gdxy.component.DynamicOperateExcelUtils;
import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.*;
import com.lablzh.gdxy.entity.admin.Parameter;
import com.lablzh.gdxy.entity.course.Course;
import com.lablzh.gdxy.entity.course.CourseApply;
import com.lablzh.gdxy.entity.course.CourseParam;
import com.lablzh.gdxy.entity.course.StudentCourse;
import com.lablzh.gdxy.entity.design.Design;
import com.lablzh.gdxy.entity.design.DesignApply;
import com.lablzh.gdxy.entity.design.StudentDesign;
import com.lablzh.gdxy.entity.practice.StudentPractice;
import com.lablzh.gdxy.repository.*;
import com.lablzh.gdxy.repository.admin.ParameterRepository;
import com.lablzh.gdxy.repository.course.CourseApplyRepository;
import com.lablzh.gdxy.repository.course.CourseRepository;
import com.lablzh.gdxy.repository.course.StudentCourseRepository;
import com.lablzh.gdxy.service.CourseService;
import com.lablzh.gdxy.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    UserDetailsUtil userDetailsUtil;
    @Autowired
    private StudentRepositoty studentRepositoty;
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private CourseApplyRepository courseApplyRepository;
    @Autowired
    private StudentCourseRepository studentCourseRepository;
    @Autowired
    private FileService fileService;
    @Autowired
    private ParameterRepository parameterRepository;
    @Autowired
    private TeacherRepository teacherRepository;

    private static final String BASEPATH = "./resources/static/";


    /**
     * 学生申请课设
     * @param id
     * @return
     */
    @Override
    public String apply(Integer id) {
        String username = userDetailsUtil.getUserDetails();
        Student student = studentRepositoty.findAllByStID(username);
        Course course = courseRepository.findById(id).orElse(null);
        System.out.println(id);
        System.out.println(course);
        assert course != null;
        if (course.getMaxApply() <= course.getApplyed()) {
            return "选题申请已达上限！";
        }
        //判断时间
        LocalDate now = LocalDate.now();
        if (now.isBefore(course.getStuChooseStart())) {
            return "选题未开始！";
        }
        if (now.isAfter(course.getStuChooseDdl())) {
            return "选题已结束！";
        }
        //初始化申请表
        CourseApply courseApply = new CourseApply();
        courseApply.setCourseID(id);
        courseApply.setCourseName(course.getCourseName());
        courseApply.setStudentUsername(student.getStID());
        courseApply.setStudentName(student.getName());
        courseApply.setTeacher(course.getTeacherUsername());
        courseApply.setCollege(course.getCollege());
        courseApply.setMajor(student.getMajor());
        courseApply.setGrade(course.getGrade());
        courseApply.setStatus("未通过");

        if (courseApplyRepository.findByStudentUsernameAndCourseID(student.getStID(), id) != null) {
            return "请不要重复申请！";
        }
        if (courseApplyRepository.save(courseApply) != null) {
            confirm(courseApply.getId());
            courseRepository.updateApplyedById(id, course.getApplyed() + 1);
            return "申请成功！";
        }
        return "申请失败！";
    }

    @Override
    @CacheEvict(cacheNames = "design", allEntries = true)
    public String undo(Integer id) {
        CourseApply courseApply = courseApplyRepository.findById(id).orElse(null);

        if (courseApply == null) {
            return "不存在该申请！";
        }
        String username = userDetailsUtil.getUserDetails();
        if (!username.equals(courseApply.getStudentUsername())) {
            return "这不是你的申请";
        }

        if ("已通过".equals(courseApply.getStatus())) {
            return "该申请已通过，不能取消！";
        }
        courseApplyRepository.deleteById(id);
        courseRepository.updateApplyedById(courseApply.getCourseID(),
                courseRepository.findById(courseApply.getCourseID()).orElse(null).getApplyed() - 1);
        return "删除成功！";
    }

    @Override
    public List<CourseApply> applyTeacher(Integer id) {
        String username = userDetailsUtil.getUserDetails();
        return courseApplyRepository.findAllByTeacherAndCourseID(username, id,Sort.by(Sort.Direction.DESC,"id"));
    }

    @Override
    public List<CourseApply> applyStudent() {
        String username = userDetailsUtil.getUserDetails();
        return courseApplyRepository.findAllByStudentUsername(username);
    }

    @Override
    public String deleteStudent(Integer id) {
        //查询课设申请
        CourseApply courseApply = courseApplyRepository.findById(id).get();
        //查询课设
        Course course = courseRepository.findById(courseApply.getCourseID()).orElse(null);
        //查询学生课设
        studentCourseRepository.deleteByCourseIDAndStudentUsername(course.getId(), courseApply.getStudentUsername());
        //对课程进行修改并保存
        Integer choosed = course.getChoosed() - 1;
        Integer toSummit = course.getToSummit() - 1;
        Integer toReview = course.getToReview() - 1;
        course.setChoosed(choosed);
        course.setToSummit(toSummit);
        course.setToReview(toReview);
        courseRepository.save(course);
        courseApplyRepository.deleteById(id);
        return "删除成功！";
    }

    /**
     * 学生申请审核通过
     * @param id
     * @return
     */
    @Override
    @CacheEvict(cacheNames = "design", allEntries = true)
    public String confirm(Integer id) {
        if (courseApplyRepository.findById(id).get().getStatus().equals("已通过")) {
            return "已审核过！";
        }
        LocalDate now = LocalDate.now();
        CourseApply courseApply = courseApplyRepository.findById(id).orElse(null);
        assert courseApply != null;
        Student student = studentRepositoty.findAllByStID(courseApply.getStudentUsername());
        Course course = courseRepository.findById(courseApply.getCourseID()).orElse(null);
        assert course != null;
        //判断
        if (course.getMaxRecv() <= course.getChoosed()) {
            return "选题确认已达上限！";
        }
        //判断时间
        if (now.isBefore(course.getTaskStart())) {
            return "教师选择未开始！";
        }
        if (now.isAfter(course.getTaskDdl())) {
            return "教师选择已结束！";
        }
        if (courseApplyRepository.updateStatusById(id, "已通过") == 0) {
            return "修改失败！";
        }
        StudentCourse studentCourse = new StudentCourse();
        //初始化学生课设表
        Integer courseid = courseApply.getCourseID();
        studentCourse.setCourseID(courseid);
        studentCourse.setTeacherUsername(course.getTeacherUsername());
        studentCourse.setResTeacherUserName(course.getResTeacherUserName());
        studentCourse.setCourseName(course.getCourseName());
        studentCourse.setStudentUsername(student.getStID());
        studentCourse.setStudentName(student.getName());
        studentCourse.setCollege(student.getCollege());
        studentCourse.setMajor(student.getMajor());
        studentCourse.setClasses(student.getClasses());
        studentCourse.setGrade(course.getGrade());
        studentCourse.setSummit("未提交");
        studentCourse.setReview("未批阅");
        if (studentCourseRepository.findByStudentUsernameAndCourseID(student.getStID(), id) != null) {
            return "请不要重复加入！";
        }
        if (studentCourseRepository.save(studentCourse) != null) {
            //已选择+1，报告未提交+1,未批阅+1
            Integer choosed = course.getChoosed() + 1;
            Integer toSummit = course.getToSummit() + 1;
            Integer toReview = course.getToReview() + 1;
            course.setChoosed(choosed);
            course.setToSummit(toSummit);
            course.setToReview(toReview);
            courseRepository.save(course);
            return "加入成功！";
        } else {
            return "加入失败！";
        }
    }

    @Override
    public String refuse(Integer id) {
        if (courseApplyRepository.updateStatusById(id, "未通过") == 0) {
            return "修改失败！";
        }
        CourseApply courseApply = courseApplyRepository.findById(id).get();
        Integer courseID = courseApply.getCourseID();
        Course course = courseRepository.findById(courseID).orElse(null);
        String studentUsername = courseApply.getStudentUsername();
        StudentCourse studentCourse = studentCourseRepository.findByCourseIDAndStudentUsername(courseID, studentUsername);

        if (studentCourse != null) {
            if ("已提交".equals(studentCourse.getSummit())) {
                return "报告已提交，不能退回！";
            } else {
                courseApply.setStatus("已拒绝");
                courseApplyRepository.save(courseApply);
            }
            assert course != null;
            //courseRepository.updateTransToSummitAndReportToSummitAndPaperToSummitAndTransToReviewAndReportToReviewAndPaperToReviewById(course.getId(),

        }
        //studentCourseRepository.deleteIDAndStudentUsername(courseApply.getCourseID(), courseApply.getStudentUsername());

        return "修改成功！";

    }

    //创建课程设计
    @Override
    @Caching(evict = {@CacheEvict(cacheNames = "course", allEntries = true),
            @CacheEvict(cacheNames = "teacher", allEntries = true)})
    public Map<String, Object> createTeam(Course course) {
//        if(courseRepository.findByCourseName(course.getCourseName())!=null){
//            return "该课设已存在！";
//        }
        Map<String, Object> map = new HashMap<>();
        // Parameter parameter = parameterRepository.findByCollegeAndGradeAndTypeAndCourseCategory(course.getCollege(), course.getGrade(), "课程设计", course.getCategory());
        Parameter parameter = parameterRepository.findByCourseMiniCategory(course.getCourseMiniCategory());
        if (parameter == null) {
            map.put("result", "未设置初始互选参数！");
            return map;
        }
        course.setGrade(parameter.getGrade());
        course.setMajor(parameter.getCourseMajor());
        course.setCollege(parameter.getCollege());
        course.setAudit("未审");
        course.setStatus("关闭");
        course.setChoosed(0);
        course.setApplyed(0);
        course.setSummited(0);
        course.setToSummit(0);
        course.setReviewed(0);
        course.setToReview(0);
        course.setMaxApply(parameter.getMaxApply());
        course.setMaxRecv(parameter.getMaxRecv());
        course.setTaskStart(parameter.getTaskStart());
        course.setTaskDdl(parameter.getTaskDdl());
        course.setStuChooseStart(parameter.getStuChooseStart());
        course.setStuChooseDdl(parameter.getStuChooseDdl());
        course.setHomeworkDdl(parameter.getHomeworkDdl());
        Course newCourse = courseRepository.save(course);
        if (newCourse != null) {
            Teacher teacher = teacherRepository.findAllByUsername(course.getTeacherUsername());
            if ("校内".equals(course.getInorout())) {
                teacher.setCourseInY(teacher.getCourseInY() + 1);
            } else {
                teacher.setCourseOutY(teacher.getCourseOutY() + 1);
            }
            teacherRepository.save(teacher);
//            Teacher resTeacher = teacherRepository.findAllByUsername(course.getResTeacherUserName());
//            String position = resTeacher.getPosition();
//            if(position == null){
//                resTeacher.setPosition("课程设计负责老师");
//            }else {
//                resTeacher.setPosition(position + " " + "课程设计负责老师");
//            }
//            teacherRepository.save(resTeacher);
            map.put("result", "添加成功！");
            map.put("courseId", newCourse.getId());
            return map;
        } else {
            map.put("result", "添加失败！");
            return map;
        }
    }


    //获取所有课程设计
    @Override
    @Cacheable(cacheNames = "course")
    public List<Course> getAllTeam() {
        return courseRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
    }

    //根据毕设id选择课程设计
    @Override
    @CacheEvict(cacheNames = "course", allEntries = true)
    public String chooseCourse(Integer id) {
        String username = userDetailsUtil.getUserDetails();
        Student student = studentRepositoty.findAllByStID(username);
        //判断时间
        LocalDate now = LocalDate.now();
        Course course = courseRepository.findById(id).orElse(null);
        assert course != null;
        if (now.isBefore(course.getStuChooseStart())) {
            return "未到任务开始时间！";
        }
        if (now.isAfter(course.getStuChooseDdl())) {
            return "任务已结束！";
        }
        StudentCourse studentCourse = new StudentCourse();
        //初始化学生课设表
        studentCourse.setCourseID(id);
        studentCourse.setTeacherUsername(course.getTeacherUsername());
        studentCourse.setResTeacherUserName(course.getResTeacherUserName());
        studentCourse.setCourseName(course.getCourseName());
        studentCourse.setStudentUsername(student.getStID());
        studentCourse.setStudentName(student.getName());
        studentCourse.setCollege(student.getCollege());
        studentCourse.setMajor(student.getMajor());
        studentCourse.setClasses(student.getClasses());
        studentCourse.setGrade(course.getGrade());
        studentCourse.setSummit("未提交");
        studentCourse.setReview("未批阅");
        if (studentCourseRepository.findByStudentUsernameAndCourseID(student.getStID(), id) != null) {
            return "请不要重复加入！";
        }
        if (studentCourseRepository.save(studentCourse) != null) {
            //已选择+1，报告未提交+1,未批阅+1
            Integer choosed = course.getChoosed() + 1;
            Integer toSummit = course.getToSummit() + 1;
            Integer toReview = course.getToReview() + 1;
            course.setChoosed(choosed);
            course.setToSummit(toSummit);
            course.setToReview(toReview);
            courseRepository.save(course);
            return "加入成功！";
        } else {
            return "加入失败！";
        }
    }

    //退选
    @Override
    @CacheEvict(cacheNames = "course", allEntries = true)
    public String deleteCouse(Integer id) {
        StudentCourse studentCourse = studentCourseRepository.findById(id).orElse(null);
//        System.out.println("");
        assert studentCourse != null;
        String username = userDetailsUtil.getUserDetails();
        if (!username.equals(studentCourse.getStudentUsername())) {
            return "这不是你的课";
        }

        Course course = courseRepository.findById(studentCourse.getCourseID()).orElse(null);
        CourseApply courseApply = courseApplyRepository.findByStudentUsernameAndCourseID(studentCourse.getStudentUsername(), studentCourse.getCourseID());
        courseApply.setStatus("已退选");
        courseApplyRepository.save(courseApply);
        if ("已提交".equals(studentCourse.getSummit())) {
            course.setSummited(course.getSummited() - 1);
        } else {
            course.setToSummit(course.getToSummit() - 1);
        }
        if ("已批阅".equals(studentCourse.getReview())) {
            course.setReviewed(course.getReviewed() - 1);
        } else {
            course.setToReview(course.getToReview() - 1);
        }
        //已选题-1
        Integer choosed = course.getChoosed() - 1;
        //已申请 -1
        Integer applied = course.getApplyed() - 1;
        course.setChoosed(choosed);
        course.setApplyed(applied);
        courseRepository.save(course);
        studentCourseRepository.deleteById(id);
        return "删除成功！";
    }

    //修改课程设计
    @Override
    @CacheEvict(cacheNames = "course", allEntries = true)
    public String updateTeam(Course course) {
        courseRepository.save(course);
        return "修改成功！";
    }

    //通过课程设计获取课程设计
    @Override
    public Course getTeamById(Integer id) {
        return courseRepository.findById(id).orElse(null);
    }

    //老师获取自己负责的课程设计
    @Override
    public List<Course> getCourseTeacher() {
        String username = userDetailsUtil.getUserDetails();
        List<Course> list = courseRepository.findAllByTeacherUsernameOrResTeacherUserName(username,username, Sort.by(Sort.Direction.DESC, "id"));

        return list;
    }

    //学生获取自己选的学生课设表
    @Override
    public List<StudentCourse> getCourseStudent() {
        String username = userDetailsUtil.getUserDetails();
        return studentCourseRepository.findAllByStudentUsername(username);
//        return courseApplyRepository.findAllByStudentUsername(username);
    }

    //通过学生课设表ID获取
    @Override
    public List<StudentCourse> getChoosed(Integer id) {
        return studentCourseRepository.findAllByCourseID(id);
    }

    @Override
    public List<StudentCourse> getStudentBySummit(String summit, Integer id) {
        return studentCourseRepository.findAllByCourseIDAndSummit(id, summit);
    }

    @Override
    public List<StudentCourse> getStudentByReview(String review, Integer id) {
        return studentCourseRepository.findAllByCourseIDAndReview(id, review);
    }

    @Override
    @CacheEvict(cacheNames = "course", allEntries = true)
    public String review(Integer id, String studentUsername, Integer score, String level, String note) {
        StudentCourse studentCourse = studentCourseRepository.findByCourseIDAndStudentUsername(id, studentUsername);
        if ("未批阅".equals(studentCourse.getReview())) {
            //已批阅+1，未批阅-1
            Course course = courseRepository.findById(id).orElse(null);
            assert course != null;
            courseRepository.updateToReviewAndReviewedById(id, course.getToReview() - 1, course.getReviewed() + 1);
        }
        studentCourseRepository.updateReportScoreAndReportLevelAndNoteAndReviewByCourseIDAndStudentUsername(id, studentUsername, score, level, note, "已批阅");
        return "修改成功！";
    }

    @Override
    public  Map<String, Object>  queryPerformance(StudentCourse studentCourse, int page) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("courseName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("college", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("studentName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains());


        Example<StudentCourse> example = Example.of(studentCourse, exampleMatcher);
        Pageable studentCoursePage = PageRequest.of(page - 1, 20, Sort.by(Sort.Direction.DESC, "id"));
        Page<StudentCourse> all = studentCourseRepository.findAll(example, studentCoursePage);
        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", all.getTotalPages());
        data.put("total", all.getTotalElements());
        data.put("studentCourse", all.getContent());
        return data;
    }


    @Override
    public void downloadExcel(StudentCourse studentCourse1,String sort,
                              HttpServletRequest request,HttpServletResponse response) throws FileNotFoundException {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("courseName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("college", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("grade", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("major", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("studentName", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("classes", ExampleMatcher.GenericPropertyMatchers.contains());


        Example<StudentCourse> example = Example.of(studentCourse1, exampleMatcher);
        List<StudentCourse> studentCourses = studentCourseRepository.findAll(example);
        //排序
        if (sort.equals("college")) {
            studentCourses.sort(Comparator.comparing(StudentCourse::getCollege));
        } else if (sort.equals("courseName")) {
            studentCourses.sort(Comparator.comparing(StudentCourse::getCourseName));
        } else if (sort.equals("studentName")) {
            studentCourses.sort(Comparator.comparing(StudentCourse::getStudentName));
        } else if (sort.equals("studentUsername")) {
            studentCourses.sort(Comparator.comparing(StudentCourse::getStudentUsername));
        } else if (sort.equals("major")) {
            studentCourses.sort(Comparator.comparing(StudentCourse::getMajor));
        } else if (sort.equals("classes")) {
            studentCourses.sort(Comparator.comparing(StudentCourse::getClasses));
        }

        DynamicOperateExcelUtils dynamicOperateExcelUtils = new DynamicOperateExcelUtils(BASEPATH + "课程设计/" + "课程设计学生汇总成绩表模板.xlsx");
        List<Map<String, Object>> datas = new ArrayList<>();
        for (StudentCourse studentCourse : studentCourses) {
            Map<String, Object> data = new HashMap<>();
            data.put("题目名称", studentCourse.getCourseName());
            data.put("院系", studentCourse.getCollege());
            data.put("专业", studentCourse.getMajor());
            data.put("班级", studentCourse.getClasses());
            data.put("学号", studentCourse.getStudentUsername());
            data.put("姓名", studentCourse.getStudentName());
            data.put("综合评分", studentCourse.getScore());
            data.put("分数成绩", studentCourse.getClassLevel());
            data.put("分数评分", studentCourse.getClassScore());
            data.put("分数比例", studentCourse.getClassProportion());
            data.put("作业报告成绩", studentCourse.getReportLevel());
            data.put("作业报告评分", studentCourse.getReportScore());
            data.put("作业报告比例", studentCourse.getReportProportion());
            datas.add(data);
        }
        dynamicOperateExcelUtils.replaceCellValue(0, 0, "课程设计学生汇总成绩表");
        if (!datas.isEmpty()) {
            dynamicOperateExcelUtils.appendRows(5, datas,
                    new String[]{"题目名称", "院系", "专业", "班级", "学号", "姓名", "综合评分", "分数成绩", "分数评分", "分数比例", "作业报告成绩", "作业报告评分", "作业报告比例"});
        } else {
            dynamicOperateExcelUtils.replaceCellValue(5, 0, "无相关记录");
        }

        dynamicOperateExcelUtils.exportExcel(new File(BASEPATH + "课程设计学生汇总成绩表.xlsx"));
        fileService.downloadFile("课程设计学生汇总成绩表.xlsx", request, response);
        fileService.deleteFile(BASEPATH + "课程设计学生汇总成绩表.xlsx");
    }

    @Override
    @CacheEvict(cacheNames = "course", allEntries = true)
    public Map<String, String> uploadReport(HttpServletRequest request, String category, Integer id) throws IOException {
        String username = userDetailsUtil.getUserDetails();
        LocalDate now = LocalDate.now();
        Map<String, String> result = fileService.summitFile(request, category, id);
        if ("上传成功！".equals(result.get("result"))) {
            //不管是第几次提交，都要更新文件名和提交时间
            studentCourseRepository.updateReportNameAndSummitTimeByIdAndStudentUsername(id, username, result.get("filename"), now);
            StudentCourse studentCourse = studentCourseRepository.findByCourseIDAndStudentUsername(id, username);
            if ("未提交".equals(studentCourse.getSummit())) {
                studentCourseRepository.updateSummitAndReviewByIdAndStudentUsername(id, username, "已提交", "未批阅");
                //课设表 未提交-1，已提交+1
                Course course = courseRepository.findById(id).orElse(null);
                assert course != null;
                courseRepository.updateToSummitAndSummitedAndToReviewById(id, course.getToSummit() - 1, course.getSummited() + 1, course.getSummited() + 1 - course.getReviewed());
            }
        }
        return result;
    }

    @Override
    @Caching(evict = {@CacheEvict(cacheNames = "course", allEntries = true),
            @CacheEvict(cacheNames = "teacher", allEntries = true)})
    public String deleteTeam(Integer id) {
        studentCourseRepository.deleteAllByCourseID(id);//删除该课程设计的学生
        Course course = courseRepository.findById(id).orElse(null);
        courseRepository.deleteById(id);
        assert course != null;
        Teacher teacher = teacherRepository.findAllByUsername(course.getTeacherUsername());
        //老师已出题数-1
        if ("校内".equals(course.getInorout())) {
            int temp = teacher.getCourseInY() - 1;
            teacher.setCourseInY(temp < 0 ? 0 : temp);
        } else {
            int temp = teacher.getCourseOutY() - 1;
            teacher.setCourseOutY(temp < 0 ? 0 : temp);
        }
        teacherRepository.save(teacher);
        return "删除成功！";
    }

    @Override
    public List<Course> getTeamStudent() {
        List<Course> courses = new ArrayList<>();
        String username = userDetailsUtil.getUserDetails();
        List<StudentCourse> studentCourses = studentCourseRepository.findAllByStudentUsername(username);
        for (StudentCourse studentCourse : studentCourses) {
            Course course = courseRepository.findById(studentCourse.getCourseID()).orElse(null);
            courses.add(course);
        }
        return courses;
    }


    @Override
    public List<CourseApply> getCourseStudentApply() {
        String username = userDetailsUtil.getUserDetails();
        return courseApplyRepository.findAllByStudentUsername(username);
    }

    //根据年级和类别修改课程设计参数
    @Override
    public String updateTeamParameter(CourseParam courseParam) {
        List<Course> courses = courseRepository.findAllByCategoryAndGrade(courseParam.getCategory(), courseParam.getGrade());
        for (Course course : courses) {
            course.setTaskStart(courseParam.getTaskStart());
            course.setTaskDdl(courseParam.getTaskDdl());
            course.setHomeworkDdl(courseParam.getHomeworkDdl());
        }
        courseRepository.saveAll(courses);
        return "参数修改成功！";
    }
}
